package com.serialize;


import com.serialize.api.StudentService;
import com.serialize.api.StudentServiceImpl;
import com.serialize.domain.Request;
import com.serialize.domain.Response;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by buguniao on 15/12/14.
 */
public class Server {
    private static StudentService service = new StudentServiceImpl();
    public static void main(String[] args) throws Exception{
        ServerSocket serverSocket = new ServerSocket(8080);
        Socket socket = null;
        while((socket=serverSocket.accept())!=null){
            ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
            Request request = (Request)in.readObject();

            String method = request.getMethod();
            Method m = StudentServiceImpl.class.getDeclaredMethod(method);
            Object rs = m.invoke(service, null);

            Response response = new Response(method,rs);

            ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
            out.writeObject(response);


        }
    }
}
